package com.dji.sample.manage.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.dji.sample.common.model.CustomClaim;
import com.dji.sample.common.model.PaginationData;
import com.dji.sample.common.model.ResponseResult;
import com.dji.sample.manage.model.entity.DevicePlayLineEntity;
import com.dji.sample.manage.service.DevicePlayLineInfoService;
import com.dji.sample.manage.service.DevicePositionService;
import com.dji.sample.manage.service.LiveInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import static com.dji.sample.component.AuthInterceptor.TOKEN_CLAIM;

/**
 * @author yjt
 * @date 2022-10-08
 * @description:
 */
@RestController
@RequestMapping("${url.manage.prefix}${url.manage.version}/devices/line")
public class DevicePlayLineController {

    @Autowired
    DevicePlayLineInfoService devicePlayLineInfoService;

    @Autowired
    DevicePositionService devicePositionService;

    @Autowired
    LiveInfoService liveInfoService;

    @GetMapping("getOssUrl")
    public ResponseResult<?> getLine(@RequestParam Long id) throws Exception {
        return ResponseResult.success(devicePlayLineInfoService.getLineOssUrl(id));
    }

    @GetMapping("list")
    public ResponseResult<PaginationData<DevicePlayLineEntity>> getLineList(@RequestParam String deviceSn, @RequestParam Long page, @RequestParam Long pageSize) {
        return ResponseResult.success(devicePlayLineInfoService.findByDeviceSn(deviceSn, page, pageSize));
    }

    @GetMapping("page")
    public ResponseResult<?> page(@RequestParam Integer page,
                                  HttpServletRequest request,
                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                  @RequestParam(name = "searchKey", required = false) String searchKey) {

        CustomClaim customClaim = (CustomClaim) request.getAttribute(TOKEN_CLAIM);
        return ResponseResult.success(devicePlayLineInfoService.page(page, pageSize, searchKey,customClaim.getOrgCode()));
    }

    @GetMapping("getPlanById")
    public ResponseResult<?> getPlanById(@RequestParam Long lineId){
        return ResponseResult.success(devicePlayLineInfoService.getByLineId(lineId));
    }

    @GetMapping("position")
    public ResponseResult<?> position(@RequestParam Long lineId) {
        DevicePlayLineEntity devicePlayLineEntity = devicePlayLineInfoService.getById(lineId);
        if (devicePlayLineEntity == null) {
            return ResponseResult.error("未查询到数据");
        }
        return ResponseResult.success(devicePositionService.list(devicePlayLineEntity.getDeviceSn(), devicePlayLineEntity.getStartPositionId(), devicePlayLineEntity.getEndPositionId()));
    }

    @GetMapping("getVideo")
    public ResponseResult<?> getVideo(@RequestParam Integer page,
                                      @RequestParam String deviceSn,
                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(name = "startTime", required = false) String startTime,
                                      @RequestParam(name = "endTime", required = false) String endTime) {

        return ResponseResult.success(liveInfoService.findByRoomId(deviceSn, page, pageSize, startTime, endTime));
    }

    @RequestMapping("rmVideo")
    public ResponseResult<?> rmVideo(@RequestParam Long id) {
        liveInfoService.deleteVideo(id);
        return ResponseResult.success();
    }

}
